package com.yc.web.servlet;

import com.yc.bean.Account;
import com.yc.service.BankBiz;
import com.yc.web.model.JsonModel;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zwei
 * @create 2024-08-09 15:42
 */
@WebServlet("/account.action")
public class AccointServlet extends BaseServlet{
    protected void findAccount(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int accountid=Integer.parseInt(req.getParameter("accountid"));
        //从spring容器中获取业务对象
        ServletContext application=req.getServletContext();
        WebApplicationContext ac= WebApplicationContextUtils.getWebApplicationContext(application);
        BankBiz bankBiz=(BankBiz) ac.getBean("bankBizImpl");
        Account a=bankBiz.findAccount(accountid);
        JsonModel jm=new JsonModel();
        jm.setCode(1);
        jm.setObj(a);
        super.writeJson(jm,resp);
    }
    protected void openAccount(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        double money=Double.parseDouble(req.getParameter("money"));
        //从spring容器中获取业务对象
        ServletContext application=req.getServletContext();
        WebApplicationContext ac= WebApplicationContextUtils.getWebApplicationContext(application);
        BankBiz bankBiz=(BankBiz) ac.getBean("bankBizImpl");
        Account a=bankBiz.openAccount(money);
        JsonModel jm=new JsonModel();
        jm.setCode(1);
        jm.setObj(a);
        super.writeJson(jm,resp);
    }
    protected void deposite(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int accountid=Integer.parseInt(req.getParameter("accountid"));
        double money=Double.parseDouble(req.getParameter("money"));
        //从spring容器中获取业务对象
        ServletContext application=req.getServletContext();
        WebApplicationContext ac= WebApplicationContextUtils.getWebApplicationContext(application);
        BankBiz bankBiz=(BankBiz) ac.getBean("bankBizImpl");

        Account a=bankBiz.deposite(accountid,money);
        JsonModel jm=new JsonModel();
        jm.setCode(1);
        jm.setObj(a);
        super.writeJson(jm,resp);
    }
    protected void withdraw(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int accountid=Integer.parseInt(req.getParameter("accountid"));
        double money=Double.parseDouble(req.getParameter("money"));
        //从spring容器中获取业务对象
        ServletContext application=req.getServletContext();
        WebApplicationContext ac= WebApplicationContextUtils.getWebApplicationContext(application);
        BankBiz bankBiz=(BankBiz) ac.getBean("bankBizImpl");
        Account a=bankBiz.withdraw(accountid,money);
        JsonModel jm=new JsonModel();
        jm.setCode(1);
        jm.setObj(a);
        super.writeJson(jm,resp);
    }
    protected void query(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int accountid=Integer.parseInt(req.getParameter("accountid"));
        //从spring容器中获取业务对象
        ServletContext application=req.getServletContext();
        WebApplicationContext ac= WebApplicationContextUtils.getWebApplicationContext(application);
        BankBiz bankBiz=(BankBiz) ac.getBean("bankBizImpl");

        Account a=bankBiz.findAccount(accountid);
        JsonModel jm=new JsonModel();
        jm.setCode(1);
        jm.setObj(a);
        super.writeJson(jm,resp);
    }
    protected void transfer(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int accountid=Integer.parseInt(req.getParameter("accountid"));
        double money=Double.parseDouble(req.getParameter("money"));
        int toaccountid=Integer.parseInt(req.getParameter("toaccountid"));
        //从spring容器中获取业务对象
        ServletContext application=req.getServletContext();
        WebApplicationContext ac= WebApplicationContextUtils.getWebApplicationContext(application);
        BankBiz bankBiz=(BankBiz) ac.getBean("bankBizImpl");

        Account a=bankBiz.transfer(accountid,money,toaccountid);
        Account b=bankBiz.findAccount(accountid);
        JsonModel jm=new JsonModel();
        jm.setCode(1);
        jm.setObj(b);
        super.writeJson(jm,resp);
    }
}
